package com.egame.manage.biz.web.controller.gc;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.egame.common.model.PageData;
import cn.egame.common.servlet.WebUtils;
import cn.egame.interfaces.EnumType.FileStatus;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.fl.FileInfo;
import cn.egame.interfaces.fl.FileUsedType;
import cn.egame.interfaces.gc.GameFileInfo;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.gc.TerminalInfo;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.cu.ManufacturerServiceImpl;
import com.egame.manage.biz.server.gc.AuditServiceImpl;
import com.egame.manage.biz.server.gc.GameFileServiceImpl;
import com.egame.manage.biz.server.gc.GameServiceImpl;
import com.egame.manage.biz.server.gc.TerminalServiceImpl;
import com.egame.manage.biz.web.controller.BaseController;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/tv/game/file")
public class NewGameFileController extends BaseController {
	public NewGameFileController() {
	}
	public NewGameFileController(HttpServletRequest req,
			HttpServletResponse resp) {
		super(req, resp);
	}

	@RequestMapping(value = "/delete", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView deleteGameFile(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);	
		long fid = WebUtils.getLong(req,"id",0);
		TerminalServiceImpl.getInstance().deleteAdaptedTerminal(appId, loginUserId, gid, fid);
		GameFileServiceImpl.getInstance().deleteGameFileById(appId, loginUserId, fid);		
		return gameFileList(gid,req,resp);
	}

	@RequestMapping(value = "/upload/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showUploadGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);		
		ModelAndView mv = new ModelAndView("/new/op/upload");		
		GameInfo bean = GameServiceImpl.getInstance().getGameInfoById(appId, loginUserId, gid);		
		mv.addObject("bean", bean);
		return mv;
	}
	
	@RequestMapping(value = "/upload", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView uploadGameFile(int gid,int file_type,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		MultipartFile file = ((MultipartHttpServletRequest)req).getFile("gameFile");		
		long efsid = ManageClient.getInstance().writeToFile(file.getInputStream(),file_type,appId,loginUserId,file.getOriginalFilename());	
		if(efsid > 0){					
			FileInfo fileInfo = ManageClient.getInstance().getFileInfo(appId, loginUserId, efsid);	
			
			GameFileServiceImpl.getInstance().setGameFile(appId, loginUserId, efsid, gid, fileInfo);		
		}
		req.setAttribute("gid", gid);
		return gameFileList(gid,req,resp);	
	}
	
	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView gameFileList(int gid,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		ModelAndView mv = new ModelAndView("/new/op/gameFileList");		
		List<GameFileInfo> list = GameFileServiceImpl.getInstance().listGameFileInfoByGid(appId, loginUserId, gid);
		mv.addObject("list", list);
		mv.addObject("gid", gid);
		return mv;
	}
		
	@RequestMapping(value = "/status/change", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView changeFileStatus(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);
		long fileid = WebUtils.getInt(req,"fileid",0);	
		int filestatus = WebUtils.getInt(req,"filestatus",0);	
		GameFileServiceImpl.getInstance().changeFileStatus(fileid, filestatus);
		GameInfo gameInfo = GameServiceImpl.getInstance().getGameInfoById(gid, loginUserId, gid);
		if(filestatus == FileStatus.testPassed.value() && gameInfo != null && gameInfo.getGameStatus() == GameStatus.auditPassed){
			GameServiceImpl.getInstance().updateGameStatus(appId, loginUserId, gid, GameStatus.testPassed.value());			
			AuditServiceImpl.getInstance().setAuditLog(gid,GameStatus.testPassed.value(),loginUserId);		
		}
		return gameFileList(gid,req,resp);	
	}
		
	@RequestMapping(value = "/terminal/adapt/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showAdaptTerminal(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);
		long fileid = WebUtils.getInt(req,"fileid",0);			
		int page = WebUtils.getInt(req,"page",0);
		int rowsOfPage = WebUtils.getInt(req,"rowsOfPage",15);
		int adapted = WebUtils.getInt(req, "adapted", 0);
		int manufactureId = WebUtils.getInt(req, "selectManufacturerId", 0);
		
		PageData pd = TerminalServiceImpl.getInstance().pageTerminalInfo(appId, loginUserId, gid, fileid, manufactureId, adapted, page, rowsOfPage);
		List<String[]> brandList = ManufacturerServiceImpl.getInstance().listManufacturerBand(appId, loginUserId);
		
		
		ModelAndView mv = new ModelAndView("/new/op/adaptTerminal");	
		mv.addObject("pd", pd);
		mv.addObject("brandList", brandList);	
		mv.addObject("gid", gid);
		mv.addObject("fileid", fileid);
		mv.addObject("adapted", adapted);
		mv.addObject("selectManufacturerId", manufactureId);
		mv.addObject("pdUrl", "/tv/game/file/terminal/adapt/show");
		return mv;
	}
//
//	@ResponseBody
//	@RequestMapping(value = "/hasAdapted", method = {RequestMethod.GET,RequestMethod.POST})
//	public long hasAdapt(HttpServletRequest req, HttpServletResponse resp) throws Exception{		
//		int gid = WebUtils.getInt(req,"gid",0);
//		return GameFileServiceImpl.getInstance().getAdaptFile(gid);
//	}
//	
	@RequestMapping(value = "/terminal/adapt/delete", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView deleteAdaptTerminal(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);
		long fileid = WebUtils.getLong(req,"fileid",0);	
		int terminalId = WebUtils.getInt(req,"terminalid",0);		
		TerminalServiceImpl.getInstance().deleteAdaptedTerminal(appId, loginUserId, gid, fileid, terminalId);
		return showAdaptTerminal(req,resp);
	}
	
	@RequestMapping(value = "/terminal/adapt", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView adaptTerminal(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);
		long fileid = WebUtils.getLong(req,"fileid",0);	
		int terminalId = WebUtils.getInt(req,"terminalid",0);	
		int terminalGroupId = 0;
		
		TerminalInfo bean = ManageClient.getInstance().getTerminalInfoById(appId, loginUserId, terminalId);
		GameFileInfo file = ManageClient.getInstance().getGameFileInfoByGId(appId, loginUserId, gid, terminalId);
		if(bean != null) terminalGroupId = bean.getTerminalGroupId();
		if(file == null){
			TerminalServiceImpl.getInstance().setTerminalFileInfo(appId,loginUserId,gid,fileid,terminalId,terminalGroupId);
			GameFileServiceImpl.getInstance().changeFileStatus(fileid, FileStatus.adaptationed.value());
		}	
		
		return showAdaptTerminal(req,resp);
	}

	@RequestMapping(value = "/set/remark", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView saveRemark(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req,"gid",0);
		long fileid = WebUtils.getLong(req,"fileid",0);	
		String remark = WebUtils.getString(req, "remark","");
		GameFileServiceImpl.getInstance().setFileRemark(fileid, remark);
		return gameFileList(gid,req, resp);
	}

	@ResponseBody
	@RequestMapping(value = "/fileinfo", method = {RequestMethod.GET,RequestMethod.POST})
	public GameFileInfo getGameFileInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		long fileid = WebUtils.getLong(req,"fileid",0);	
		GameFileInfo gfile = ManageClient.getInstance().getGameFileInfoById(appId, loginUserId, fileid);
		return gfile;
	}
	
	@ResponseBody
	@RequestMapping(value = "/save", method = {RequestMethod.GET,RequestMethod.POST})
	public String saveFile(String name,HttpServletRequest req, HttpServletResponse resp)throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		resp.setContentType("text/plain; charset=UTF-8");
		MultipartFile file = ((MultipartHttpServletRequest)req).getFile(name);			
		long efsid = ManageClient.getInstance().writeToFile(file.getInputStream(),FileUsedType.FILE,appId,loginUserId,file.getOriginalFilename());	
		if(efsid>0){			
			String[] tmp = GameServiceImpl.getInstance().getFilePath(appId, loginUserId, efsid);
			if(tmp != null && tmp.length>=2){
				return efsid + "," + tmp[0] + "," + tmp[1];
			}			
		}
		return "-1,-1,-1";
	}
}
